<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
<script>

    var arr = [6, 5, 4, 3, 2]
    console.log(arr);
    
    // - 0-1, 1-2, 2-3, 3-4     0~arr.length-1-i
    // - 0-1, 1-2, 2-3          0~arr.length-1-i
    // - 0-1, 1-2               0~arr.length-1-i
    // - 0-1                    0~arr.length-1-i

    // 4行，循环执行4次，length为5，计数器从0开始，到3即可，小于length-1

    for(var i=0;i<arr.length-1;i++){
        for(var j=0;j<arr.length-1-i;j++){
            if(arr[j] > arr[j+1]){
                var x = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = x;
            }
        }
    }
    
    console.log(arr);
</script>
</html>